<template>
  <strong-search
    v-bind="$attrs"
    :filter-list="options"
    @search="handleSearch"></strong-search>
</template>

<script setup lang="ts">
import { type FilterItem } from '../../strong-search'
import type { CloudFilterBarState } from '../types'
// const state = defineModel<CloudFilterBarState>('state', { required: true })
const props = withDefaults(
  defineProps<{
    options: FilterItem[]
  }>(),
  {
    options: () => []
  }
)
const emit = defineEmits(['search'])

const handleSearch = (obj: CloudFilterBarState) => {
  const keys = props.options.reduce((acc, cur) => {
    if (cur.popover === 'date') {
      const { startKey, endKey } = cur.popoverOption!.dateProps!
      if (cur.popoverOption?.dateProps?.singleField) {
        acc.push(cur.field) // 只向后端发送一个字段
      } else {
        acc.push(startKey, endKey)
      }
    } else {
      acc.push(cur.field)
    }
    return acc
  }, [] as string[])
  emit('search', obj, keys)
}
</script>
